package jianzhioffer.string;

/**
 * 替换空格
 * 替换字符串中每一个空格替换为 "%20"
 * Created by yzy on 2019-11-05 19:40
 */
public class ReplaceSpace {

    public static void main(String[] args) {
        StringBuffer bu = new StringBuffer("hello world antony  my");
        System.out.println(replaceSpace(bu));
    }

    public static String replaceSpace(StringBuffer str) {
        if(str==null){
            return null;
        }
        int i=0;
        while(i<str.length()){
            if(str.charAt(i) == ' '){
                str.replace(i,i+1, "%20");
                i = i+3;
            }else{
                i = i+1;
            }
        }
        return str.toString();
    }
}
